// pages/weather/weather.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    now:'',
    address:''
  },

  /**
   * 生命周期函数--监听页面加载
   */

  data: {
    cardCur: 0,
    swiperList: [{
      id: 0,
      type: 'image',
      url: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.YKgUis_VZOwMD5J_tnU4MgHaEJ?pid=ImgDet&rs=1'
    }, {
      id: 1,
        type: 'image',
        url: 'https://epaper.gzdaily.cn/news/image/2022-06-27/T3/220616jb_b.jpg',
    }, {
      id: 2,
      type: 'image',
      url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-c8f4ae1b98d4758e3e7d095ebbde9038_1440w.jpg%3Fsource%3D172ae18b&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659699841&t=a49e23922718133701facea5bd2df333'
    }, {
      id: 3,
      type: 'image',
      url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.nfu.edu.cn%2Fimages%2F2022-06%2F599b98a555da4dd4a34642d8aecd87f9.jpg&refer=http%3A%2F%2Fwww.nfu.edu.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659699895&t=19f8e5f5d7315bb0af49cd472eed784f'
    }, {
      id: 4,
      type: 'image',
      url: 'https://ts1.cn.mm.bing.net/th/id/R-C.f709888d54e51df57a027b1f73eb65c7?rik=szkucoMZxwjK%2bw&riu=http%3a%2f%2fzt.gaoxiaojob.com%2fzsdxnfxy2103.jpg&ehk=hj4uR%2b%2bw7TxHGAes1gOOHmJ510do9TzyfkbFD5KGMGw%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1'
    }, {
      id: 5,
      type: 'image',
      url: 'https://yjs.ahau.edu.cn/__local/4/84/20/0DF2BA1CA3A8FDD8BA1AD15A12B_1A9FFB20_28F21.jpg?e=.jpg'
    }, {
      id: 6,
      type: 'image',
      url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp0.itc.cn%2Fq_70%2Fimages03%2F20210313%2F5e43bbc6366048bca1176c5601b0e08e.jpeg&refer=http%3A%2F%2Fp0.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659699752&t=47fc201fb658bac15421e6352a205925'
    }],
  },
  onLoad() {
    this.towerSwiper('swiperList');
    // 初始化towerSwiper 传已有的数组名即可
  },
  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },


  onLoad(options) {
    var that = this
    // 1. 先获取用户的当前位置信息
    wx.getLocation({
      type: 'wgs84',// 地球坐标系
      success (res) {
        console.log(res)
        const latitude = res.latitude 
        const longitude = res.longitude
        // 2. 获取当前的结构化地址
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo?parameters', //逆地理编码url
          data: {
            key: '682fce538eb09c02fe00d9268bf7f974',
            location: longitude+','+latitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data)
            const Component=res.data.regeocode.addressComponent
            that.setData({
              address:Component.province+Component.city+Component.district
            })
          }
        })
        // 3. 再获取当前的天气信息
        wx.request({
          url: 'https://devapi.qweather.com/v7/weather/now?', //仅为示例，并非真实的接口地址
          data: {
            key: '8f63da7427bd4d86a48716e1f9a604cf',
            location: longitude+','+latitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data)
            that.setData({
              now:res.data.now
            })
          }
        })

      }
     })
     



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }

  
})


